package com.atguigu.rabbitmq.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
* 消费者，目的是专门接收消息的
* */
public class Consumer {

    /*队列名称*/
    public static final String QUEUE_NAME = "hello";

    /*接收消息*/
    public static void main(String[] args) throws IOException, TimeoutException {
        /*创建连接工厂*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.161.128");
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        /*在连接里面创建信道*/
        Channel channel = connection.createChannel();

        /*消息接收声明,接收消息的回调*/
        DeliverCallback deliverCallback=(consumerTag,message)->{
            /*在这个里面接收消息*/
            /*这里的message打印的是消息的地址*/
            System.out.println(new String(message.getBody()));
        };
        /*取消消息时的回调*/
        CancelCallback cancelCallback=consumerTag->{
            System.out.println("消息消费被中断");
        };


        /*消费者在信道里面获取消息,消费消息
        * 参数说明
        * 1.消费那个队列
        * 2.消费成功之后是否要自动应答 要自动应答：true  手动应答：false
        * 3.消费者成功消费消息的回调
        * 4.消费者取消消费的回调
        * */
        channel.basicConsume(QUEUE_NAME, true,deliverCallback,cancelCallback);
    }

}